<template>
	<view class="page">
		<view class="form-content">
			<u-form :model="formData" ref="uForm" class="uForm" label-position="top">
				<u-form-item prop="majorRelationPerson" label="投保人/被保险人" label-width="auto">
					<u-input v-model="formData.majorRelationPerson" placeholder="请输入投保人/被保险人" />
				</u-form-item>
				<u-form-item prop="insuranceCompanyName" label="保险公司名称" label-width="auto">
					<u-input v-model="formData.insuranceCompanyName" placeholder="请输入保险公司名称" />
				</u-form-item>
				<u-form-item prop="insuranceProductName" label="保险产品名称" label-width="auto">
					<u-input v-model="formData.insuranceProductName" placeholder="请输入保险产品名称" />
				</u-form-item>
				<u-form-item prop="coverageAmount" label="保障金额" label-width="auto">
					<u-input v-model="formData.coverageAmount" placeholder="请输入保障金额" type="digit" />
					<view slot="right">元</view>
				</u-form-item>
				<u-form-item prop="insuranceAmount" label="保费金额" label-width="auto">
					<u-input v-model="formData.insuranceAmount" placeholder="请输入保费金额" type="digit" />
					<view slot="right">元</view>
				</u-form-item>
				<u-form-item prop="insuranceStartDate" label="保险生效日期" label-width="auto">
					<dateTime v-model="formData.insuranceStartDate" :showLabel="false" :showArrow="false"
						:required="false" :disabled="true" className="new-date-picker" placeholder="请选择保险生效日期"
						jnpfKey="date"></dateTime>
				</u-form-item>
				<u-form-item prop="insuranceEndDate" label="保险失效日期" label-width="auto">
					<dateTime v-model="formData.insuranceEndDate" :showLabel="false" :showArrow="false"
						:required="false" :disabled="true" className="new-date-picker" placeholder="请选择保险失效日期"
						jnpfKey="date"></dateTime>
				</u-form-item>
				<u-form-item prop="attachment" label="附件信息" label-width="auto" label-position="top">
					<JNPFUpload v-model="attendanceFileList" :fileSize='2' :limit='10' :showLabel="false"
						@uploading="uploading">
					</JNPFUpload>
				</u-form-item>
			</u-form>
		</view>
		<view class="save-bottom">
			<view class="fixed-btn save-padding">
				<u-button type="primary" shape="circle" :disabled="btnDisabled" @click="onSubmit">确定</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	const apiUrl = "/api/Jiushi/Customerinsurancecoverage";
	export default {
		components: {},
		name: 'Majorinsurancecoverage',
		data() {
			return {
				formData: {
					id: undefined,
					customerId: undefined,
					type: 2,
					majorRelationPerson: undefined,
					insuranceCompanyName: undefined,
					insuranceProductName: undefined,
					coverageAmount: undefined,
					insuranceAmount: undefined,
					insuranceStartDate: undefined,
					insuranceEndDate: undefined,
					attachment: undefined,
				},
				attendanceFileList: [],
				btnDisabled: false
			}
		},
		onLoad(options) {
			if (options.cid) {
				this.formData.customerId = options.cid
			}
			if (options.id) {
				this.formData.id = options.id
				this.init()
			}
		},
		methods: {
			uploading(flag) {
				this.btnDisabled = flag
			},
			/* 初始化请求 */
			init() {
				this.$helper.ajax({
					url: apiUrl + "/" + this.formData.id,
					method: "GET",
					contentType: "application/json",
					success: res => {
						this.formData = res.data.data
						if (this.formData.attachment) {
							this.attendanceFileList = res.data.data.attachment.split(',').map(item => {
								let tempItem = {
									url: item
								}
								return tempItem
							})
						}
					}
				});
			},
			/* 表单提交 */
			onSubmit() {
				this.formData.attachment = this.attendanceFileList.map(item => {
					return item.url
				}).join(',')
				let url = apiUrl;
				let method = "POST";
				if (this.formData.id) {
					url = url + "/" + this.formData.id;
					method = "PUT";
				}
				this.$helper.ajax({
					url: url,
					method: method,
					contentType: "application/json",
					data: this.formData,
					success: res => {
						this.$helper.msg('保存成功', 'success');
						uni.navigateBack();
					}
				});
			},
		}
	}
</script>

<style>

</style>